﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DaoDataEntity;
using MySql.Data.MySqlClient;
using DBAccess;
using LOG;

namespace CacheAccess
{
   public class PlayerManager:Init
    {
       public MasterinfoEntity getPlayerInfoBySign(string sign)
       {
           var p = from a in CacheManager.m_Masterinfo where a.Value.PlatformSign == sign select a;
           return initAttribute((MasterinfoEntity)p);
       }
       public MasterinfoEntity getPlayerInfoByID(int ID)
       {
           if (CacheManager.m_Masterinfo.ContainsKey(ID))
               return initAttribute( CacheManager.m_Masterinfo[ID]);
           else
               return null;
       }
       public MasterinfoEntity checkLogin(string loginKey)
       {
           var p = from a in CacheManager.m_Masterinfo where a.Value.LoginKey == loginKey select a;
           return initAttribute((MasterinfoEntity)p);
       }
       public MasterinfoEntity addNewPlayer(MasterinfoEntity newPlayer)
       {
          
               Common com = new Common();
               MySqlCommand cmd = new MySqlCommand();
               MySqlConnection con = com.getConnection();
               con.Open();
               MySqlTransaction trans = con.BeginTransaction();
               cmd.Connection = trans.Connection;
               cmd.CommandText = "insert into masterinfo ";
               cmd.Transaction = trans;
               int masterid = 0;
               int baseinfoID = 0;
               try
               {
                  bool cmd1= com.executeCommandTran(con, cmd,ref masterid);
                   if(masterid<=0)
                   {
                       Log.Add("Master表数据插入错误,请核查.", -796);
                       throw new Exception("masher表和baseinfo表事务插入失败");
                   }
                  cmd.CommandText = "ins";
                  cmd.Parameters.Clear();
                  bool cmd2 = com.executeCommandTran(con, cmd,ref baseinfoID);
                  if (baseinfoID <= 0)
                  {
                      Log.Add("baseinfo表数据插入错误,请核查.", -797);
                      throw new Exception("masher表和baseinfo表事务插入失败");
                  }
                  if (cmd1 && cmd2)
                  {
                      trans.Commit();
                      newPlayer.MasterInfoID = masterid;
                      newPlayer.Baseinfo._BaseInfoID = baseinfoID;
                      CacheManager.m_Masterinfo.Add(newPlayer.MasterInfoID, newPlayer);
                      CacheManager.m_Baseinfo.Add(newPlayer.BaseInfoID, newPlayer.Baseinfo);
                      return newPlayer;
                  }
                  else
                  {
                      trans.Rollback();
                      return null;
                  }
                   
               }
               catch (Exception ex)
               {
                   trans.Rollback();
                   Log.Add(ex.ToString(), -795);
                   return null;
               }
               finally
               {
                   con.Close();
                   con.Dispose();
                   cmd.Dispose();
               }

       }

       public MasterinfoEntity updatePlayerInfo(MasterinfoEntity player)
       {
           Common com = new Common();
           MySqlCommand cmd = new MySqlCommand();
           MySqlConnection con = com.getConnection();
           con.Open();
           MySqlTransaction trans = con.BeginTransaction();
           cmd.Connection = trans.Connection;
           cmd.CommandText = "update 1 ";
           cmd.Transaction = trans;
           int masterid = 0;
           int baseinfoID = 0;
           try
           {
               bool cmd1 = com.executeCommandTran(con, cmd, ref masterid);
              
               cmd.CommandText = "update 2";
               cmd.Parameters.Clear();
               bool cmd2 = com.executeCommandTran(con, cmd, ref baseinfoID);

               if (cmd1 && cmd2)
               {
                   trans.Commit();
                   if (CacheManager.m_Masterinfo.ContainsKey(player.MasterInfoID))
                   {
                       CacheManager.m_Masterinfo[player.MasterInfoID] = player;
                   }
                   else

                       CacheManager.m_Masterinfo.Add(player.MasterInfoID, player);

                   if (CacheManager.m_Baseinfo.ContainsKey(player.Baseinfo._BaseInfoID))
                   {
                       CacheManager.m_Baseinfo[player.Baseinfo._BaseInfoID] = player.Baseinfo;
                   }
                   else
                       CacheManager.m_Baseinfo.Add(player.Baseinfo._BaseInfoID, player.Baseinfo);
               }
               else
                   trans.Rollback();
               return null;
           }
           catch (Exception ex)
           {
               trans.Rollback();
               Log.Add(ex.ToString(), -794);
               return null;
           }
           finally
           {
               con.Close();
               con.Dispose();
               cmd.Dispose();
           }
       }

       public MasterinfoEntity initAttribute(MasterinfoEntity player)
       {
           if (player._WLGJMIN == 0 || player._WLFYMIN == 0)
           {
               player._WLGJMIN = player.Baseinfo._Power * 3;
               player._WLGJMAX = player.Baseinfo._Power * 4;
               player._WLFYMIN = player._WLGJMIN*10/100-player.Baseinfo._Strong*2;
               player._WLFYMAX = player._WLGJMIN * 15 / 100 + player.Baseinfo._Strong*2;
               player._SUDU = player.Baseinfo._Quick * 2;
               
               //装备附加属性
           }
           return player;
       }
    }
}
